﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Controls.Primitives;

namespace ManYor
{
    /// <summary>
    /// Page弹出通知的对象
    /// </summary>
    public class PageNotify 
    {
        private Popup pop;
        private TextBlock tbString;
        private Timer timer;
        private PhoneApplicationPage _page;
        private Storyboard fadeInAnimal;
        private Storyboard fadeOutAnimal;
        private bool isLock;

        /// <summary>
        /// 构造函数
        /// </summary>
        public PageNotify(PhoneApplicationPage page)
        {
            this._page = page;
            this._page.Loaded += new RoutedEventHandler(ManYorPhoneApplicationPage_Loaded);           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ManYorPhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            InitializePopup();
            timer = new Timer(Timer_Tick);
        }

        /// <summary>
        /// 初始化弹出框
        /// </summary>
        private void InitializePopup()
        {
            pop = new Popup();
            pop.HorizontalOffset = 0;
            pop.VerticalOffset = 0;

            Border border = new Border();
            border.CornerRadius = new CornerRadius(8);
            border.Background = new SolidColorBrush(Colors.Black);
            border.Opacity = 0.75;
            border.Width = 300;
            border.Height = 60;
            border.Margin = new Thickness((480 - border.Width) / 2,
                                        (800 - border.Height) / 2,
                                        (480 - border.Width) / 2,
                                        (800 - border.Height) / 2);


            Canvas cas = new Canvas();
            cas.Opacity = 0.75;
            cas.Width = 300;
            cas.Height = 60;

            border.Child = cas;

            tbString = new TextBlock();
            tbString.HorizontalAlignment = HorizontalAlignment.Center;
            tbString.FontSize = 23;
            tbString.Foreground = new SolidColorBrush(Colors.White);
            tbString.TextWrapping = TextWrapping.Wrap;
            tbString.Text = "";
            Size tbSize = tbString.RenderSize;

            tbString.Margin = new Thickness(20, 20,20,20);
            StackPanel sp = new StackPanel();
            sp.Width = 300;
            sp.Margin = new Thickness(0, 0, 0, 0);
            sp.Children.Add(tbString);

            cas.Children.Add(sp);
            pop.Child = border;

            fadeInAnimal = CretaeStoryboard(0, 0.75);
            fadeOutAnimal = CretaeStoryboard(0.75, 0);
            Storyboard.SetTarget(fadeInAnimal, border);
            Storyboard.SetTarget(fadeOutAnimal, border);
            fadeOutAnimal.Completed += new EventHandler(FadeOutCompleted);
        }

        /// <summary>
        /// 显示popup
        /// </summary>
        /// <param name="str"></param>
        public void ShowPopup(string str)
        {
            if (isLock)
                return;
            isLock = true;
            tbString.Text = str;
            pop.IsOpen = true;
            fadeInAnimal.Begin();
            timer.Change(1300, 0);
        }

        /// <summary>
        /// 隐藏popup
        /// </summary>
        public void HidePopup()
        {
            this._page.Dispatcher.BeginInvoke(new Action(() =>
            {
                fadeOutAnimal.Begin();  
            }));           
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void FadeOutCompleted(object sender, EventArgs e)
        {
            this._page.Dispatcher.BeginInvoke(new Action(() =>
            {
                isLock = false;
                pop.IsOpen = false;
            }));   
        }
        /// <summary>
        /// 计时器
        /// </summary>
        private void Timer_Tick(object state)
        {
            HidePopup();            
        }

        /// <summary>
        /// 创建时间画板
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        private Storyboard CretaeStoryboard(double from, double to)
        {
            Storyboard sb = new Storyboard();
            DoubleAnimation animation = new DoubleAnimation
            {
                Duration = TimeSpan.FromMilliseconds(500),
                From = from,
                To = to,
                EasingFunction = new QuarticEase { EasingMode = EasingMode.EaseOut }
            };
            Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
            sb.Children.Add(animation);
            return sb;
        }
    }
}
